<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDrawer">关 闭</el-button>
      <multiuse-button type="primary" v-show="type != 'show'" :click="commitHandle">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { numberCheck } from '@/utils/validate';

import { getFiDiscountRule, addFiDiscountRule, updateFiDiscountRule } from '@/api/omFinance/PriceDiscountManage/DiscountRuleMaintain';

export default {
  name: 'SonModel',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'show'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom()
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (this.type == 'add') {
        this.formList.find(x => x.fieldName == 'status').initialValue = 10031001;
        this.formList.find(x => x.fieldName == 'startDate').initialValue = moment().format('YYYY-MM-DD');
        this.formList.find(x => x.fieldName == 'endDate').initialValue = moment().format('9999-12-31');
      } else {
        this.formList.forEach(x => {
          if (x.fieldName == 'discountValue') {
            x.initialValue = this.rowData['discountValue'];
            if (this.rowData['discountMethod'] == 60331002) {
              this.formList.find(x => x.fieldName == 'discountValue').label = '折扣值（%）';
              this.formList.find(x => x.fieldName == 'discountValue').min = 0;
              this.formList.find(x => x.fieldName == 'discountValue').max = 100;
              return;
            }
            this.formList.find(x => x.fieldName == 'discountValue').label = '折扣值';

            this.formList.find(x => x.fieldName == 'discountValue').min = -999999999;
            this.formList.find(x => x.fieldName == 'discountValue').max = 999999999;
          } else {
            x.initialValue = this.rowData[x.fieldName];
          }
        });
      }
      if (this.type == 'show') {
        this.formList.map(x => {
          if (x.rules) {
            x.rules[0].required = false;
          }
        });
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },

    async commitHandle() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let saveData = Object.assign({}, this.rowData, this.saveData);
        console.log(saveData, '---提报的数据---');
        let res = {};
        if (this.type === 'add') {
          res = await addFiDiscountRule(saveData);
        }
        if (this.type === 'edit') {
          res = await updateFiDiscountRule(saveData);
        }
        if (res.resultCode === 200) {
          await this.closeDrawer('refresh');
        }
      }
    },
    closeDrawer(type) {
      this.$emit('close', false, type);
    },
    selectChangeHandle() {},
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008'),
          rules: [{ required: true, message: '请选择动力形式', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '折扣规则名称',
          fieldName: 'ruleName',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入折扣规则名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '折扣种类',
          fieldName: 'discountType',
          placeholder: '请选择',
          itemList: this.createDictList('6032'),
          rules: [{ required: true, message: '请选择折扣种类', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '折扣计算方式',
          fieldName: 'discountMethod',
          placeholder: '请选择',
          itemList: this.createDictList('6033'),
          rules: [{ required: true, message: '请选择折扣计算方式', trigger: 'change' }],
          change: val => {
            if (val == 60331002) {
              this.formList.find(x => x.fieldName == 'discountValue').label = '折扣值（%）';
              this.formList.find(x => x.fieldName == 'discountValue').initialValue = '';
              this.formList.find(x => x.fieldName == 'discountValue').min = 0;
              this.formList.find(x => x.fieldName == 'discountValue').max = 100;
              return;
            }
            this.formList.find(x => x.fieldName == 'discountValue').label = '折扣值';

            this.formList.find(x => x.fieldName == 'discountValue').min = -999999999;
            this.formList.find(x => x.fieldName == 'discountValue').max = 999999999;
          }
        },
        {
          type: 'INPUT',
          label: '折扣值',
          fieldName: 'discountValue',
          min: -999999999,
          max: 999999999,
          placeholder: '请输入',
          rules: [
            { required: true, message: '请输入折扣值', trigger: 'blur' },
            { trigger: 'blur', validator: numberCheck }
          ]
        },
        {
          type: 'SELECT',
          label: '启用标志',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003'),
          rules: [{ required: true, message: '请选择启用标志', trigger: 'change' }]
        },
        {
          type: 'DATE',
          label: '生效日期',
          fieldName: 'startDate',
          dateType: 'exactdate',
          placeholder: '请选择',
          minDateTime: moment().add(-1, 'days'),
          rules: [{ required: true, message: '请选择生效日期', trigger: 'change' }],
          change: val => {
            if (val) {
              this.formList.find(x => x.fieldName == 'endDate').initialValue = '';
              this.formList.find(x => x.fieldName == 'endDate').minDateTime = moment(new Date(val)).add(-1, 'days');
            }
          }
        },
        {
          type: 'DATE',
          label: '失效日期',
          fieldName: 'endDate',
          dateType: 'exactdate',
          placeholder: '请选择',
          minDateTime: moment().add(-1, 'days'),
          maxDateTime: moment().format('9999-12-31'),
          rules: [{ required: true, message: '请选择失效日期', trigger: 'change' }]
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          selfCols: 3,
          placeholder: '请输入'
        }
      ];
    }
  }
};
</script>
